package com.unbox.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;
import com.unbox.admin.entity.RoleMenu;
import com.unbox.admin.mapper.RoleMenuMapper;
import com.unbox.admin.service.IRoleMenuService;
import com.unbox.common.table.UnBoxPage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * <业务实现类
 * </p>
 *
 * @author 王振宇
 * @since 2021-03-06
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

    @Override
    public RoleMenu get(RoleMenu roleMenu) {
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(roleMenu.getId())) {
            lambdaQueryWrapper.eq(RoleMenu::getId, roleMenu.getId());
        }
        if (!ObjectUtils.isEmpty(roleMenu.getCreateTime())) {
            lambdaQueryWrapper.eq(RoleMenu::getCreateTime, roleMenu.getCreateTime());
        }
        if (!ObjectUtils.isEmpty(roleMenu.getMenuId())) {
            lambdaQueryWrapper.eq(RoleMenu::getMenuId, roleMenu.getMenuId());
        }
        if (!ObjectUtils.isEmpty(roleMenu.getRoleId())) {
            lambdaQueryWrapper.eq(RoleMenu::getRoleId, roleMenu.getRoleId());
        }
       return super.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<RoleMenu> getList(RoleMenu roleMenu) {
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(roleMenu.getId())) {
            lambdaQueryWrapper.eq(RoleMenu::getId, roleMenu.getId());
        }
        if (!ObjectUtils.isEmpty(roleMenu.getCreateTime())) {
            lambdaQueryWrapper.eq(RoleMenu::getCreateTime, roleMenu.getCreateTime());
        }
        if (!ObjectUtils.isEmpty(roleMenu.getMenuId())) {
            lambdaQueryWrapper.eq(RoleMenu::getMenuId, roleMenu.getMenuId());
        }
        if (!ObjectUtils.isEmpty(roleMenu.getRoleId())) {
            lambdaQueryWrapper.eq(RoleMenu::getRoleId, roleMenu.getRoleId());
        }
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public UnBoxPage<List<RoleMenu>> getPageList(RoleMenu roleMenu, int pageNo, int pageSize) {
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(roleMenu.getId())) {
            lambdaQueryWrapper.eq(RoleMenu::getId, roleMenu.getId());
        }
        if (!ObjectUtils.isEmpty(roleMenu.getCreateTime())) {
            lambdaQueryWrapper.eq(RoleMenu::getCreateTime, roleMenu.getCreateTime());
        }
        if (!ObjectUtils.isEmpty(roleMenu.getMenuId())) {
            lambdaQueryWrapper.eq(RoleMenu::getMenuId, roleMenu.getMenuId());
        }
        if (!ObjectUtils.isEmpty(roleMenu.getRoleId())) {
            lambdaQueryWrapper.eq(RoleMenu::getRoleId, roleMenu.getRoleId());
        }
        Page<RoleMenu> page = new Page<>(pageNo,pageSize);
        IPage<RoleMenu> pageResult = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        UnBoxPage<List<RoleMenu>> unBoxPage = new UnBoxPage<>();
        unBoxPage.setPageNo(pageNo);
        unBoxPage.setPageSize(pageSize);
        unBoxPage.setSumPageCount(pageResult.getPages());
        unBoxPage.setSumDataCount(pageResult.getTotal());
        unBoxPage.setDatas(pageResult.getRecords());
        return unBoxPage;
    }

    @Override
    public List<RoleMenu> getIdsList(List<Integer> ids) {
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(RoleMenu::getId, ids);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public List<RoleMenu> getRoleIdsList(List<Integer> ids) {
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(RoleMenu::getRoleId, ids);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean save(RoleMenu entity) {
        return super.save(entity);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveBatch(List<RoleMenu> entityList) {
        return super.saveBatch(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean update(RoleMenu roleMenu) {
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoleMenu::getId, roleMenu.getId());
        return super.update(roleMenu, lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBatchById(List<RoleMenu> entityList) {
        return super.updateBatchById(entityList);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean remove(RoleMenu roleMenu) {
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoleMenu::getId, roleMenu.getId());
        return super.remove(lambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByIds(List<Integer> idList) {
        return super.removeByIds(idList);
    }
}
